package com.heima.wemedia.controller.v1;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.wemedia.service.WmMaterialService;
import io.swagger.models.auth.In;
import org.apache.hbase.thirdparty.io.netty.util.internal.StringUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/v1/material")
public class WmMaterialController {

    @Resource
    private WmMaterialService wmMaterialService;

    @PostMapping("/upload_picture")
    public ResponseResult<WmMaterial> uploadPicture(@RequestParam("multipartFile") MultipartFile multipartFile) {
        if(StrUtil.equals(Convert.toStr(multipartFile.getSize()), Convert.toStr(0))) {
            return ResponseResult.errorResult(AppHttpCodeEnum. SERVER_ERROR);
        }
        return this.wmMaterialService.uploadPicture(multipartFile);
    }

    @PostMapping("/list")
    public ResponseResult<List<WmMaterial>> queryList(@RequestBody WmMaterialDto wmMaterialDto){

        return this.wmMaterialService.queryList(wmMaterialDto);
    }

    /**
     * 根据id 删除
     * @param id
     * @return
     */
    @GetMapping("/del_picture/{id}")
    public  ResponseResult deleteById(@PathVariable("id") Integer id){
        this.wmMaterialService.removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 取消收藏也就是 先查后改动
     * @param id
     * @return
     */
    @GetMapping("/cancel_collect/{id}")
    public ResponseResult cancelCollect(@PathVariable("id")Integer id){
        WmMaterial wmMaterial = this.wmMaterialService.getById(id);
        if(ObjectUtil.isEmpty(wmMaterial)){
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
        wmMaterial.setIsCollection((short)0);
        this.wmMaterialService.updateById(wmMaterial);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @GetMapping("/collect/{id}")
    public  ResponseResult collect(@PathVariable("id") Integer id){
        WmMaterial wmMaterial = this.wmMaterialService.getById(id);
        if(ObjectUtil.isEmpty(wmMaterial)){
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
        wmMaterial.setIsCollection((short)1);
        this.wmMaterialService.updateById(wmMaterial);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}
